package richbar.com.richbar.activity.me;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.base.BaseListAdapter;
import richbar.com.richbar.bean.BaseBean;
import richbar.com.richbar.bean.FreePwdBean;
import richbar.com.richbar.bean.UserInfoResult;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.NoScrollListview;

/**
 * Created by QiYang on 2018/11/16.
 */

public class SelectFreePwdAmountAc extends BaseActivity {
    @BindView(R.id.listView)
    NoScrollListview listView;
    private MyAdapter myAdapter;
    private LoadingDialog loadingDialog;
    private int clickPos = 0;
    FreePwdBean freePwdBean;

    @Override
    public int initLayout() {
        return R.layout.ac_select_free_pwd_amount;
    }

    @Override
    public void initTitle() {
        new TitleHelp(this).setTitle("选择免密金额");
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this, 0);
    }

    @Override
    public void initData() {
        getUserInfo();
    }

    @OnClick(R.id.besure)
    public void onViewClicked() {
        loadingDialog.show();
        NetWorkUtil.setOpenNoPwdPay(freePwdBean.getData().getList().get(clickPos).getAmountID() + "", new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);
                if (baseBean.getCode().equals("200"))
                    finish();
                ToastUtil.showToast(SelectFreePwdAmountAc.this, baseBean.getMsg());
                loadingDialog.dismiss();
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                loadingDialog.dismiss();
                ToastUtil.showToast(SelectFreePwdAmountAc.this, msg);
            }
        });


    }

    private void getUserInfo() {
        loadingDialog.show();
        NetWorkUtil.getNoPwdPayList(new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                freePwdBean = MyGson.getInstance().fromJson(data, FreePwdBean.class);
                myAdapter = new MyAdapter(SelectFreePwdAmountAc.this, freePwdBean.getData().getList());
                listView.setAdapter(myAdapter);
                loadingDialog.dismiss();
            }

            @Override
            public void onError(String msg) {
                super.onError(msg);
                loadingDialog.dismiss();
                ToastUtil.showToast(SelectFreePwdAmountAc.this, msg);
            }
        });
    }

    class MyAdapter extends BaseListAdapter<FreePwdBean.DataBean.ListBean> {


        public MyAdapter(Context context, List<FreePwdBean.DataBean.ListBean> data) {
            super(context, data);
        }

        @Override
        public View convert(final int i, View view, ViewGroup viewGroup, FreePwdBean.DataBean.ListBean bean) {
            View v = getLayoutInflater().inflate(R.layout.item_single, null);
            ImageView select = v.findViewById(R.id.select);
            TextView title = v.findViewById(R.id.title);
            title.setText(bean.getPrice() + "元/笔");
            if (clickPos == i) {
                select.setVisibility(View.VISIBLE);
            } else {
                select.setVisibility(View.GONE);
            }
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickPos = i;
                    notifyDataSetChanged();
                }
            });
            return v;
        }
    }
}
